package com.mikgonz.farkle.server;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;

public class FarkleService {

	private static Objectify ofy = new DAO().ofy();
	private static final Logger log = Logger.getLogger(FarkleService.class
			.getName());

	// //////////////////////////// Friendship \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

	public static Friendship createFriendship() {
		Friendship f = new Friendship();
		ofy.put(f);
		return f;
	}

	public static Friendship readFriendship(Long id) {
		return ofy.get(Friendship.class, id);
	}

	public static Friendship updateFriendship(Friendship fr) {
		ofy.put(fr);
		return fr;
	}

	public static void deleteFriendship(Friendship friendship) {
		ofy.delete(friendship);
	}

	public static List<Friendship> queryFriendshipsByString(String username) {
		List<Friendship> list1 = ofy.query(Friendship.class)
				.filter("originalName", username).list();
		;
		List<Friendship> list2 = ofy.query(Friendship.class)
				.filter("otherName", username).list();
		list1.addAll(list2);
		return list1;
	}

	public static List<Friendship> queryFriendshipsByLong(Long id) {
		List<Friendship> list1 = ofy.query(Friendship.class)
				.filter("originalUserId", id).list();
		;
		List<Friendship> list2 = ofy.query(Friendship.class)
				.filter("otherUserId", id).list();
		list1.addAll(list2);
		return list1;
	}

	public static Friendship startFriendship(String originalName,
			Long originalId, String otherName) throws IOException {

		log.info("originalName: " + originalName);
		log.info("originalId: " + originalId.toString());
		log.info("otherName: " + otherName);

		Query<User> q = ofy.query(User.class).filter("name", otherName);
		User u = q.get();

		if (u == null)
			return null;

		Friendship fr = new Friendship(originalName, originalId, otherName,
				u.getId());

		ofy.put(fr);

		XtifyHelper.sendFriendRequest(originalName, u.getXtifyKey(),
				Long.toString(fr.getId()));

		return fr;
	}

	public static User acceptOrRejectFriendship(Long id, boolean accept)
			throws IOException {

		Friendship fr = ofy.get(Friendship.class, id);

		if (fr == null)
			return null;

		if (accept) {
			System.err.println("Friendship Accepted");
			User original = ofy.get(User.class, fr.getOriginalUserId());
			XtifyHelper.notifyFriendRequestAccepted(fr.getOtherName(),
					original.getXtifyKey(), fr.getOtherUserId());
			ofy.delete(fr);
			return original;
		} else {
			System.err.println("Friendship Rejected");
			User original = ofy.get(User.class, fr.getOriginalUserId());
			XtifyHelper.notifyFriendRequestRejected(fr.getOtherName(),
					original.getXtifyKey());
			ofy.delete(fr);
			return null;
		}

	}

	// //////////////////////////// USER \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

	public static User createUser() {
		User u = new User();
		ofy.put(u);
		return u;
	}

	public static User readUser(Long id) {
		return ofy.get(User.class, id);
	}

	public static User updateUser(User user) {
		ofy.put(user);
		return user;
	}

	public static void deleteUser(User user) {
		ofy.delete(user);
	}

	public static User logIn(String username, String password, Long id,
			String xtifyKey) {

		User byId = null;

		if (id != null) {
			byId = ofy.get(User.class, id);
		}

		if (byId != null) {
			final boolean success = byId.getName().equals(username)
					&& byId.getPassword().equals(password);
			// update xtifykey
			if (success) {
				byId.setXtifyKey(xtifyKey);
				ofy.put(byId);
			}

			if (success)
				return byId;
		}

		// Finding user by Id didn't work. Query for the name

		Query<User> q = ofy.query(User.class).filter("name", username);

		User byQuery = q.get();

		final boolean success = byQuery != null
				&& byQuery.getPassword().equalsIgnoreCase(password);
		// update xtifykey
		if (success) {
			byQuery.setXtifyKey(xtifyKey);
			ofy.put(byQuery);
		}

		return (success) ? byQuery : null;
	}

	public static User register(String username, String password,
			String xtifyKey) {

		Query<User> q = ofy.query(User.class).filter("name", username);
		User u = q.get();

		if (u != null)
			return null;

		u = new User(username, password, xtifyKey);
		ofy.put(u);
		return u;
	}

	public static String checkAvailability(String username) {
		final boolean available = ofy.query(User.class)
				.filter("name", username).get() == null;
		return (available) ? "true" : "false";
	}

	// ////////////////////////////////// CHAT \\\\\\\\\\\\\\\\\\\\\\\\\\\\

	public static void sendChatMessage(Long fromId, Long toId, String body)
			throws IOException {

		final User receiver = ofy.get(User.class, toId);
		final User sender = ofy.get(User.class, fromId);

		XtifyHelper
				.sendChatMesg(sender.getName(), receiver.getXtifyKey(), body);

		System.err.println("Chat Sent");

	}

}
